package com.bsj.power.sdk.web.api;

import cn.hutool.core.io.FileUtil;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.mq.sdk.SdkLoginDevInfo;
import com.bsj.power.sdk.cache.ChildDeviceCache;
import com.bsj.power.sdk.config.hksdk.alarm.AlarmSet;
import com.bsj.power.sdk.util.VideoConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 * @author yinhao
 * @version 1.0
 * @description 设备控制API
 * @date 2024/8/28
 */
@Slf4j
@RestController
@RequestMapping("/command")
public class CommandApi {

    /**
     * 附件地址
     */
    @Value("${annexFile.path}")
    private String path;

    private static final String CMD_ON = "on";
    private static final String CMD_OFF = "off";

    /**
     * AI报警布防/撤防
     * @param devId 设备ID
     * @param changer 布防开关 on:开启 off:关闭
     * @return
     */
    @GetMapping(value = "/ai/alarm")
    public JsonResult aiAlarmSet(Long devId, String changer) {
        JsonResult result = JsonResult.success();
        if (devId == null || changer == null) {
            throw new ApiException(JsonResultEnum.PARAM_ERROR);
        }
        SdkLoginDevInfo dev = ChildDeviceCache.getInfoByDevId(devId);
        if (dev == null) {
            throw new ApiException(JsonResultEnum.DEVICE_NOT_EXIST);
        }
        if (changer.equalsIgnoreCase(CMD_ON)) {
            //开启布防
            AlarmSet.arrange(dev, result);
        } else if (changer.equalsIgnoreCase(CMD_OFF)) {
            AlarmSet.withdraw(dev);
            result.setMsg("撤防成功");
        }
        return result;
    }

    /**
     * 文件转码
     * @param filePath
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/file/transcode")
    public JsonResult transcode(String filePath) throws Exception{
        if (filePath == null) {
            throw new ApiException(JsonResultEnum.PARAM_ERROR);
        }
        //文件的全路径
        String fileTranPath = path + filePath;
        long size = FileUtil.size(new File(fileTranPath));
        if (size == 0) {
            log.warn("无效文件，无需转码: {}", fileTranPath);
            throw new ApiException(JsonResultEnum.NO_FILE_FAIL);
        }
        //先备份原始文件,替换后缀
        String[] split = filePath.split("\\.");
        String replaceStr = filePath.replaceAll(split[1], "flv");
        String copyFileTranPath = path + replaceStr;
        // 复制文件,true参数表示如果目标文件已经存在，那么将会被覆盖
        File file = FileUtil.copy(fileTranPath, copyFileTranPath, true);
        if (!file.exists()) {
            throw new ApiException(JsonResultEnum.TRANS_FILE_FAIL);
        }
        //删除前端上传的文件
        FileUtil.del(fileTranPath);
        //进行转码
        boolean convertWebP = VideoConvertUtil.convertWebP(copyFileTranPath, fileTranPath);
        if (!convertWebP) {
            //转码失败
            throw new ApiException(JsonResultEnum.TRANS_FILE_FAIL);
        }
        return JsonResult.success(filePath);
    }
}
